package com.example.hrandroidapp.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hrandroidapp.R;

public class UIUtil {


    /**
     * Get default progress bar.
     * 
     * @param context
     * @return
     */
    
    public static TextView createTransparentSpace(Context context) {
        TextView space = new TextView(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params.height = 2;

        space.setLayoutParams(params);
        space.setBackgroundColor(context.getResources().getColor(R.color.transparent));
        return space;

    }

    /**
     * GEt the divider view.
     * 
     * @param context
     * @param parent
     * @return
     */
    public static View getDivider(Context context, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.white_divider, parent, false);
    }

    /**
     * Get simple section title view.
     * 
     * @param context
     * @param title
     * @param parent
     * @param layout if null then use default.
     * @return
     */
    public static View getSectionTitleRow(Context context, String title, ViewGroup parent, Integer layout) {
        if (layout == null || layout == 0) {
            // Default layout
            layout = R.layout.simplesectiontitle;
        }
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layout, parent, false);

        ((TextView) view.findViewById(R.id.textView_row)).setText(title);

        return view;
    }

    /**
     * Resize all the views with the tag "resize" based on the ratio.
     * 
     * Some of view size is designed based on small screen(like 320 dp) device, but you want the size bigger on large screen devices, just
     * call this, make sure you tag the views "resize".
     * 
     * @param v
     * @param ratio
     */
    public static void adjustViewSize(ViewGroup v, float ratio) {
        for (int n = 0; n < v.getChildCount(); n++) {
            View vin = v.getChildAt(n);
            if (vin.getTag() != null && vin.getTag().equals("resize")) {
                vin.getLayoutParams().height *= ratio;
                vin.getLayoutParams().width *= ratio;
            }
            if (vin instanceof ViewGroup) {
                adjustViewSize((ViewGroup) vin, ratio);
            }

        }
    }

    public static float getPxFromDp(Context ctx, float dp) {
        return dp * ctx.getResources().getDisplayMetrics().density;
    }

}
